import { queryInfo, update, queryFirePage, queryPropertyPage, queryMaintenancePage, queryRelationList, queryOwnerList } from './service';
import { routerRedux } from 'dva/router';

import { queryDictList } from '@/services/dict'

export default {
  namespace: 'owner',

  state: {
    list: { data: [] },
    details: null,
    cond: {},
    dict: {},
    editDetails: {},
    fireList: { data: [] },
    propertyList: { data: [] },
    maintenanceList: { data: [] },
    relationList: {},
    ownerList: {},
  },

  effects: {
    *queryInfo(_, { call, put }) {
      const response = yield call(queryInfo);
      yield put({ type: 'setInfo', payload: response })
    },
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    },
    *queryEditInfo({ payload }, { call, put }) {
      const response = yield call(queryInfo);
      const items = {};
      payload.keys.forEach(item => {
        items[item] = response[item]
      })
      yield put({ type: 'setEditInfo', payload: items })
    },
    *update({ payload }, { call, put }) {
      yield call(update, payload);
      yield put(routerRedux.push('/data/owner'));
    },
    *queryFirePage({ payload }, { call, put }) {
      const { list = [], total } = yield call(queryFirePage, payload);
      yield put({ type: 'setFirePage', payload: { list, total: +total } })
    },
    *queryPropertyPage({ payload }, { call, put }) {
      const { list = [], total } = yield call(queryPropertyPage, payload);
      yield put({ type: 'setPropertyPage', payload: { list, total: +total } })
    },
    *queryMaintenancePage({ payload }, { call, put }) {
      const { list = [], total } = yield call(queryMaintenancePage, payload);
      yield put({ type: 'setMaintenancePage', payload: { list, total: +total } })
    },
    *init({ payload }, { put }) {
      yield put({ type: 'upload/setImgValue', payload: { url: payload.url } });
      yield put({ type: 'queryDict', payload: payload.dict });
      yield put({ type: 'location/queryList' });
    },
    *queryRelationList({ payload }, { call, put }) {
      const list = yield call(queryRelationList, payload);
      yield put({ type: 'setRelationList', payload: list })
    },
    *queryOwnerList({ payload }, { call, put }) {
      const list = yield call(queryOwnerList, payload);
      yield put({ type: 'setOwnerList', payload: Array.isArray(list) ? list.map((v, ind) => ({ ...v, ind })) :[] })
    },
  },

  reducers: {
    setInfo(state, action) {
      return {
        ...state,
        details: action.payload
      }
    },
    setEditInfo(state, action) {
      return {
        ...state,
        editDetails: action.payload
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    },
    setRoleTree(state, action) {
      return {
        ...state,
        ...action.payload
      }
    },
    setFirePage(state, action) {
      return {
        ...state,
        fireList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setPropertyPage(state, action) {
      return {
        ...state,
        propertyList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setMaintenancePage(state, action) {
      return {
        ...state,
        maintenanceList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setRelationList(state, action) {
      return {
        ...state,
        relationList: action.payload,
      }
    },
    setOwnerList(state, action) {
      return {
        ...state,
        ownerList: action.payload,
      }
    },
  }
}